package com.cw.jeeyt.config.process;

import java.io.IOException;
import java.util.List;

import javax.sql.DataSource;

import org.flowable.common.engine.impl.cfg.IdGenerator;
import org.flowable.common.spring.AutoDeploymentStrategy;
import org.flowable.engine.ProcessEngine;
import org.flowable.idm.engine.IdmEngineConfiguration;
import org.flowable.idm.engine.configurator.IdmEngineConfigurator;
import org.flowable.job.service.impl.asyncexecutor.AsyncExecutor;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.FlowableAutoDeploymentProperties;
import org.flowable.spring.boot.FlowableHttpProperties;
import org.flowable.spring.boot.FlowableMailProperties;
import org.flowable.spring.boot.FlowableProperties;
import org.flowable.spring.boot.ProcessEngineAutoConfiguration;
import org.flowable.spring.boot.app.FlowableAppProperties;
import org.flowable.spring.boot.eventregistry.FlowableEventRegistryProperties;
import org.flowable.spring.boot.idm.FlowableIdmProperties;
import org.flowable.spring.boot.process.FlowableProcessProperties;
import org.flowable.spring.boot.process.Process;
import org.flowable.spring.boot.process.ProcessAsync;
import org.flowable.spring.boot.process.ProcessAsyncHistory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import com.cw.jeeyt.service.impl.process.FlowIdentityServiceImpl;

@Configuration
@EnableConfigurationProperties(FlowableProperties.class)
public class FlowDataSourceProcessEngineAutoConfiguration extends ProcessEngineAutoConfiguration {

    @Value("${activiti.diagram.activityFontName}")
    private String activityFontName;

    @Value("${activiti.diagram.labelFontName}")
    private String labelFontName;

    @Value("${activiti.diagram.annotationFontName}")
    private String annotationFontName;

    public FlowDataSourceProcessEngineAutoConfiguration(FlowableProperties flowableProperties,
        FlowableProcessProperties processProperties, FlowableAppProperties appProperties,
        FlowableIdmProperties idmProperties, FlowableEventRegistryProperties eventProperties, FlowableMailProperties mailProperties,
        FlowableHttpProperties httpProperties,FlowableAutoDeploymentProperties autoDeploymentProperties) {
        super(flowableProperties, processProperties, appProperties, idmProperties,eventProperties, mailProperties, httpProperties,autoDeploymentProperties);
    }

    @Override
    @Bean
    @ConditionalOnMissingBean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(DataSource dataSource, PlatformTransactionManager platformTransactionManager,
        @Process ObjectProvider<IdGenerator> processIdGenerator,
        ObjectProvider<IdGenerator> globalIdGenerator,
        @ProcessAsync ObjectProvider<AsyncExecutor> asyncExecutorProvider,
        @ProcessAsyncHistory ObjectProvider<AsyncExecutor> asyncHistoryExecutorProvider,
        ObjectProvider<List<AutoDeploymentStrategy<ProcessEngine>>> processEngineAutoDeploymentStrategies) throws IOException {

        SpringProcessEngineConfiguration conf = super.springProcessEngineConfiguration(dataSource, platformTransactionManager,
            processIdGenerator,
            globalIdGenerator,
            asyncExecutorProvider,
            asyncHistoryExecutorProvider,
            processEngineAutoDeploymentStrategies);
        conf.setIdmEngineConfigurator(idmEngineConfigurator(dataSource));
        conf.setActivityFontName(activityFontName);
        conf.setLabelFontName(labelFontName);
        conf.setAnnotationFontName(annotationFontName);

        return conf;
    }

    @Bean
    public IdmEngineConfigurator idmEngineConfigurator(DataSource dataSource) {
        IdmEngineConfiguration idmEngineConfiguration = new IdmEngineConfiguration();
        idmEngineConfiguration.setDataSource(dataSource);
        idmEngineConfiguration.setIdmIdentityService(new FlowIdentityServiceImpl());

        IdmEngineConfigurator idmEngineConfigurator = new IdmEngineConfigurator();
        idmEngineConfigurator.setIdmEngineConfiguration(idmEngineConfiguration);
        return idmEngineConfigurator;
    }
}
